var express = require("express");
var router = express.Router();
const bodyParser = require("body-parser");
const controllerBlock = require("../controller/block");
const { sectionPath } = require("../util");
const engine = require("../liquid/index");

// // 渲染积木块
// router.post("/render/:temp/block/:block", bodyParser.json(), function (req, res, next) {
//     var block = req.params.block.replace(/\:/g, "/");
//     var blockFile = `./src/theme/${req.params.temp}/sections/${block}.liquid`;
//     engine
//         .renderFile(blockFile, {
//             theme: req.params.temp,
//             section: req.body.default
//         })
//         .then((html) => {
//             res.send(`<div id="section-${req.body.id}">${html}</div>`);
//         });
// });

// // 更新积木块信息
// router.post("/block", bodyParser.json(), function (req, res, next) {
//     controllerBlock.addBlock(req.body);
//     res.json({ code: 200, data: "", msg: "添加成功" }).end();
// });

// // 获取积木块信息
// router.get("/block", bodyParser.json(), function (req, res, next) {
//     res.json({ code: 200, data: controllerBlock.getBlocks(req.query), msg: "添加成功" }).end();
// });

// // 删除积木块
// router.delete("/block", bodyParser.json(), function (req, res, next) {
//     controllerBlock.delBlock(req.body);
//     res.json({ code: 200, data: "", msg: "删除成功" }).end();
// });

// //更新积木块信息
// router.put("/block", bodyParser.json(), function (req, res, next) {
//     controllerBlock.updateBlock(req.body);
//     res.json({ code: 200, data: "", msg: "更新成功" }).end();
// });

// router.put("/blockAll", bodyParser.json(), function (req, res, next) {
//     controllerBlock.updateBlockAll(req.body);
//     res.json({ code: 200, data: "", msg: "更新成功" }).end();
// });

// router.post("/addBlock", bodyParser.json(), function (req, res, next) {
//     controllerBlock.addBlock(req.body);
//     res.json({ code: 200, data: "", msg: "添加成功" }).end();
// });

// // 渲染积木块
// router.get("/block/allConfig", bodyParser.json(), function (req, res, next) {
//     const data = getConfig();
//     res.json({ data: data }).end();
// });

// // 获取所有模版积木块配置
// router.get("/themeAll", bodyParser.json(), function (req, res, next) {
//     res.json({ data: controllerBlock.getThemeAll() }).end();
// });

// 获取所有模版积木块配置
router.get("/cardAll", bodyParser.json(), function (req, res, next) {
    res.json({ data: controllerBlock.getCardAll() }).end();
});

router.post("/create/theme", bodyParser.json(), async function (req, res, next) {
    try {
        const ret = await controllerBlock.createTheme(req.body);
        res.json(ret).end();
    } catch (error) {
        console.log(error);
        res.json({ code: 500, msg: "服务器异常" }).end();
    }
});

/* 渲染积木块 */
router.post("/render/block/:block", bodyParser.json(), function (req, res, next) {
    var block = req.params.block.replace(/\:/g, "/");
    
    req.body.default['block_id'] = req.body.id;
    var html = engine.renderFileSync(sectionPath(block, "liquid"), {
        theme: req.params.temp,
        section: req.body.default
    });
    res.send(`${html}`);
});

module.exports = router;
